package doctor

import (
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"sort"
)

func (b Base) GetBgFilter(c *gin.Context, req *GetBgFilterRequest) *GetBgFilterResponse {
	if req.Month > 12 {
		handler.ReplyError(c, http.StatusBadRequest, "invalid month")
		return nil
	}
	if req.PatientId == 0 {
		handler.ReplyError(c, http.StatusBadRequest, "invalid ID")
		return nil
	}
	filters, err := model.GetFilters(config.DB, req.Year, req.Month, req.PatientId)
	if err != nil {
		handler.Errorf(c, err.Error())
		return nil
	}
	sort.Slice(filters, func(i, j int) bool {
		return filters[i] < filters[j]
	})
	return &GetBgFilterResponse{Filters: filters}
}

type GetBgFilterRequest struct {
	PatientId uint `json:"patient_id"`
	Year      uint `json:"year"`
	Month     uint `json:"month"`
}

type GetBgFilterResponse struct {
	Filters []int `json:"filters"`
}
